package com.itheima.reggie.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.service.CategoryService;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;


    /**
     * 分页查询
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/page")
    public R<Page>page(int page,int pageSize){
        log.info("page={},pageSize={}",page,pageSize);
        //接受前端来的数据保存在Page方法中
        Page <Category>pageinfo = new Page(page,pageSize);
        LambdaQueryWrapper<Category> qw = new LambdaQueryWrapper<>();
        qw.orderByDesc(Category::getSort);
        //调用Page方法
        categoryService.page(pageinfo,qw);
        return R.success(pageinfo);
    }

    /**
     * 修改套餐
     * @param request
     * @param category
     * @return
     */
    @PutMapping
    public R<String>update(HttpServletRequest request, @RequestBody Category category){
        log.info(category.toString());
        //拿到管理者ID
        Long category1 = (Long) request.getSession().getAttribute("employee");
        //
        category.setUpdateTime(LocalDateTime.now());
        log.info(category1.toString());
        category.setUpdateUser(category1);
        categoryService.updateById(category);
        return R.success("修改成功");
    }

    /**
     *  添加菜品分类
     * @return
     */
    @PostMapping
    public R<String> seve(HttpServletRequest request,@RequestBody Category category){
        log.info(category.toString());
        //找到管理者ID
//        Long employee = (Long) request.getSession().getAttribute("employee");
//        //设置创建时间
//        category.setCreateTime(LocalDateTime.now());
//        //设置修改时间
//        category.setUpdateTime(LocalDateTime.now());
//        //设置修改人
//        category.setUpdateUser(employee);
//        //设置创建人
//        category.setCreateUser(employee);
        //添加菜品
        categoryService.save(category);
        return R.success("添加成功");
    }

    /**
     * 根据ID删除
     * @return
     */
    @DeleteMapping
    public R<String> delete(Long id){
        log.info("id={}",id);
        categoryService.remove(id);
        return R.success("删除成功");
    }

    /**
     * 根据条件查询分类数据
     * @param category
     * @return
     */
    @GetMapping("/list")
    public R<List<Category>>save(Category category){
        log.info("category={}",category);
        LambdaQueryWrapper<Category> qw = new LambdaQueryWrapper<>();
        //添加条件
        qw.eq(category.getType()!=null,Category::getType,category.getType());
        //添加排序条件
        qw.orderByDesc(Category::getSort).orderByDesc(Category::getCreateTime);

        List<Category> list = categoryService.list(qw);

        return  R.success(list);
    }
}
